package com.cennavi.bigdata.baotou.test;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.cennavi.bigdata.baotou.util.DateUtils;
import com.mongodb.*;

import java.text.DecimalFormat;
import java.util.Date;
import java.util.Random;

import static java.lang.System.out;

/**
 * Created by zhouxiaolong on 2017/3/30.
 */
public class MakeIndexTestData {

    private MongoClient mongoClient = null;
    private DB db = null;
    private DBCollection roadIndexCol = null;

    public MakeIndexTestData(){
        //获取 mongo 的连接
        ServerAddress serverAddress = new ServerAddress("123.207.109.239", 27017);
        mongoClient = new MongoClient(serverAddress);
        db = mongoClient.getDB("baotou_traffic");
        roadIndexCol = db.getCollection("road_index_history");
    }
    private void makeTestData(Date todayStartTime,Date todayEndTime) throws Exception {
        //删除已存在的表
//        System.out.println("drop collection:road_index_history");
//        roadIndexCol.drop();
        //插入测试数据
        out.println("insert test data......");
        double max = 10.0;
        double min = 1.0;
        while(DateUtils.earlierThan(todayStartTime,todayEndTime)){
            String strings = "{\"dccode\":\"150298\",\"dcname\":\"市中心\",\"latlon\":\"110.09785,40.58643\",\"c_index\":4.3,\"avgspeed\":28.2}";
            JSONObject jsonObject = JSONObject.parseObject(strings);
            //{"dccode":"150298","dcname":"市中心","latlon":"110.09785,40.58643","c_index":4.3,"avgspeed":28.2}
            if(jsonObject.getDouble("c_index").compareTo(new Double(10))>0){
                jsonObject.put("c_index",10.0);
            }
            jsonObject.put("update_time",DateUtils.DateFormatUnit.DATE_AND_MIN.getDateStr(todayStartTime));
            jsonObject.put("c_index", Double.valueOf(new DecimalFormat("#.00").format(nextDouble(min,max))));
            jsonObject.put("avgspeed",Double.valueOf(new DecimalFormat("#.00").format(nextDouble(min,max))));
            roadIndexCol.insert(BasicDBObject.parse(jsonObject.toJSONString()));
            out.println(DateUtils.DateFormatUnit.DATE_AND_MIN.getDateStr(todayStartTime));
            todayStartTime = DateUtils.addMinute(todayStartTime,5);
        }
        mongoClient.close();
        out.println("process done!!");
    }

    public static void main(String[] args) {
        MakeIndexTestData it = new MakeIndexTestData();
        String nowformat = "2017-04-07";
        Date todayStartTime = DateUtils.DateFormatUnit.DATE_AND_MIN.getDateByStr(nowformat+" 00:00");
        Date todayEndTime = DateUtils.DateFormatUnit.DATE_AND_MIN.getDateByStr(nowformat+" 23:59");
        try {
            it.makeTestData(todayStartTime,todayEndTime);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static double nextDouble(final double min, final double max) throws Exception {
        if(min==max){return min;}
        return min + ((max - min) * new Random().nextDouble());
    }

}
